﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projeto_Build_0.Models;

namespace Projeto_Build_0.Controllers
{
    public class EventoController : Controller
    {
        private JardimEntities db = new JardimEntities();
            

        //
        // GET: /Evento/Details/5
        [Authorize(Roles = "Funcionario, EE")]

        public ViewResult Details(int id)
        {
            Evento evento = db.Eventos.Find(id);
            return View(evento);
        }

        //
        // GET: /Evento/Create

        [Authorize(Roles = "Funcionario")]

        public ActionResult Create()
        {

            List<int> listaAnos = (from p in db.SemanaEscolars.ToList()
                                select p.Ano).Distinct().ToList();
            
            List<int> listaSemanas = (from p in db.SemanaEscolars.ToList()
                                   select p.NumeroSemana).Distinct().ToList();

            List<string> listaSalas = (from p in db.SemanaEscolars.ToList()
                                      select p.Sala.NomeSala).Distinct().ToList();

            List<string> listaDias = (from p in db.DiaDaSemanas.ToList()
                                    select p.Dia).Distinct().ToList();

            //Select all the items from the list and place them into a SelectList object.
            //Store the list in ViewData.

            ViewData["Dias"] = new SelectList(from items in listaDias select items);
            ViewData["Anos"] = new SelectList(from items in listaAnos select items);
            ViewData["Semanas"] = new SelectList(from items in listaSemanas select items);
            ViewData["Salas"] = new SelectList(from items in listaSalas select items);
            return View();
        }

        //
        // POST: /Evento/Create
        [Authorize(Roles = "Funcionario")]
        [HttpPost]
        public ActionResult Create(Evento evento)
        {
            Evento novoEvento = new Evento
            {
                NomeEvento = evento.NomeEvento,
                Descricao = evento.Descricao,
                HoraFim = evento.HoraFim,
                HoraInicio = evento.HoraInicio,
                DiaEscolar = (from p in db.DiaEscolars.ToList()
                              where (p.DiaDaSemana.Dia == evento.DiaEscolar.DiaDaSemana.Dia
                              && p.SemanaEscolar.Sala.NomeSala == evento.DiaEscolar.SemanaEscolar.Sala.NomeSala
                              && p.SemanaEscolar.NumeroSemana == evento.DiaEscolar.SemanaEscolar.NumeroSemana
                              && p.SemanaEscolar.Ano == evento.DiaEscolar.SemanaEscolar.Ano)
                              select p).FirstOrDefault()
            };


            
            if (ModelState.IsValid)
            {
                db.Eventos.Add(novoEvento);
                db.SaveChanges();
                return RedirectToAction("Index", "SemanaEscolar", new { Nsemana = 1, Ano = 2011 });
            }

            return View(evento);
        }

        //
        // GET: /Evento/Edit/5

        [Authorize(Roles = "Funcionario")]
        public ActionResult Edit(int id)
        {
            Evento evento = db.Eventos.Find(id);
            return View(evento);
        }

        //
        // POST: /Evento/Edit/5

        [HttpPost]
        [Authorize(Roles = "Funcionario")]
        public ActionResult Edit(Evento evento)
        {
            if (ModelState.IsValid)
            {
                db.Entry(evento).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", "Evento", new { id = evento.EventoId });
            }
            return View(evento);
        }

        //
        // GET: /Evento/Delete/5
        [Authorize(Roles = "Funcionario")]
        public ActionResult Delete(int id)
        {
            Evento evento = db.Eventos.Find(id);
            return View(evento);
        }

        //
        // POST: /Evento/Delete/5
        [Authorize(Roles = "Funcionario")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Evento evento = db.Eventos.Find(id);
            db.Eventos.Remove(evento);
            db.SaveChanges();
            return RedirectToAction("Index", "SemanaEscolar", new { Nsemana = 1, Ano = 2011 });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }        

    }
}